package com.bear.newsset.dao;

import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import com.bear.newsset.dao.entity.NewsDataSqlEntity;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "NEWS_DATA_SQL_ENTITY".
*/
public class NewsDataSqlEntityDao extends AbstractDao<NewsDataSqlEntity, Long> {

    public static final String TABLENAME = "NEWS_DATA_SQL_ENTITY";

    /**
     * Properties of entity NewsDataSqlEntity.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property QueryId = new Property(1, Long.class, "queryId", false, "QUERY_ID");
        public final static Property NewsId = new Property(2, String.class, "newsId", false, "NEWS_ID");
        public final static Property ItemType = new Property(3, int.class, "itemType", false, "ITEM_TYPE");
        public final static Property Title = new Property(4, String.class, "title", false, "TITLE");
        public final static Property Url = new Property(5, String.class, "url", false, "URL");
        public final static Property MainLable = new Property(6, String.class, "mainLable", false, "MAIN_LABLE");
        public final static Property ChildLable = new Property(7, String.class, "childLable", false, "CHILD_LABLE");
        public final static Property AppCode = new Property(8, String.class, "appCode", false, "APP_CODE");
        public final static Property SourceRegion = new Property(9, String.class, "sourceRegion", false, "SOURCE_REGION");
        public final static Property PublishDate = new Property(10, int.class, "publishDate", false, "PUBLISH_DATE");
    }

    private DaoSession daoSession;

    private Query<NewsDataSqlEntity> queryNewsDataEntity_DataQuery;

    public NewsDataSqlEntityDao(DaoConfig config) {
        super(config);
    }
    
    public NewsDataSqlEntityDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"NEWS_DATA_SQL_ENTITY\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"QUERY_ID\" INTEGER," + // 1: queryId
                "\"NEWS_ID\" TEXT UNIQUE ," + // 2: newsId
                "\"ITEM_TYPE\" INTEGER NOT NULL ," + // 3: itemType
                "\"TITLE\" TEXT," + // 4: title
                "\"URL\" TEXT," + // 5: url
                "\"MAIN_LABLE\" TEXT," + // 6: mainLable
                "\"CHILD_LABLE\" TEXT," + // 7: childLable
                "\"APP_CODE\" TEXT," + // 8: appCode
                "\"SOURCE_REGION\" TEXT," + // 9: sourceRegion
                "\"PUBLISH_DATE\" INTEGER NOT NULL );"); // 10: publishDate
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"NEWS_DATA_SQL_ENTITY\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, NewsDataSqlEntity entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long queryId = entity.getQueryId();
        if (queryId != null) {
            stmt.bindLong(2, queryId);
        }
 
        String newsId = entity.getNewsId();
        if (newsId != null) {
            stmt.bindString(3, newsId);
        }
        stmt.bindLong(4, entity.getItemType());
 
        String title = entity.getTitle();
        if (title != null) {
            stmt.bindString(5, title);
        }
 
        String url = entity.getUrl();
        if (url != null) {
            stmt.bindString(6, url);
        }
 
        String mainLable = entity.getMainLable();
        if (mainLable != null) {
            stmt.bindString(7, mainLable);
        }
 
        String childLable = entity.getChildLable();
        if (childLable != null) {
            stmt.bindString(8, childLable);
        }
 
        String appCode = entity.getAppCode();
        if (appCode != null) {
            stmt.bindString(9, appCode);
        }
 
        String sourceRegion = entity.getSourceRegion();
        if (sourceRegion != null) {
            stmt.bindString(10, sourceRegion);
        }
        stmt.bindLong(11, entity.getPublishDate());
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, NewsDataSqlEntity entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long queryId = entity.getQueryId();
        if (queryId != null) {
            stmt.bindLong(2, queryId);
        }
 
        String newsId = entity.getNewsId();
        if (newsId != null) {
            stmt.bindString(3, newsId);
        }
        stmt.bindLong(4, entity.getItemType());
 
        String title = entity.getTitle();
        if (title != null) {
            stmt.bindString(5, title);
        }
 
        String url = entity.getUrl();
        if (url != null) {
            stmt.bindString(6, url);
        }
 
        String mainLable = entity.getMainLable();
        if (mainLable != null) {
            stmt.bindString(7, mainLable);
        }
 
        String childLable = entity.getChildLable();
        if (childLable != null) {
            stmt.bindString(8, childLable);
        }
 
        String appCode = entity.getAppCode();
        if (appCode != null) {
            stmt.bindString(9, appCode);
        }
 
        String sourceRegion = entity.getSourceRegion();
        if (sourceRegion != null) {
            stmt.bindString(10, sourceRegion);
        }
        stmt.bindLong(11, entity.getPublishDate());
    }

    @Override
    protected final void attachEntity(NewsDataSqlEntity entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public NewsDataSqlEntity readEntity(Cursor cursor, int offset) {
        NewsDataSqlEntity entity = new NewsDataSqlEntity( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1), // queryId
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // newsId
            cursor.getInt(offset + 3), // itemType
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // title
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // url
            cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // mainLable
            cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // childLable
            cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // appCode
            cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9), // sourceRegion
            cursor.getInt(offset + 10) // publishDate
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, NewsDataSqlEntity entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setQueryId(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1));
        entity.setNewsId(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setItemType(cursor.getInt(offset + 3));
        entity.setTitle(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setUrl(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setMainLable(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.setChildLable(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
        entity.setAppCode(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setSourceRegion(cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9));
        entity.setPublishDate(cursor.getInt(offset + 10));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(NewsDataSqlEntity entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(NewsDataSqlEntity entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(NewsDataSqlEntity entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "data" to-many relationship of QueryNewsDataEntity. */
    public List<NewsDataSqlEntity> _queryQueryNewsDataEntity_Data(Long queryId) {
        synchronized (this) {
            if (queryNewsDataEntity_DataQuery == null) {
                QueryBuilder<NewsDataSqlEntity> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.QueryId.eq(null));
                queryBuilder.orderRaw("T.'PUBLISH_DATE' DESC");
                queryNewsDataEntity_DataQuery = queryBuilder.build();
            }
        }
        Query<NewsDataSqlEntity> query = queryNewsDataEntity_DataQuery.forCurrentThread();
        query.setParameter(0, queryId);
        return query.list();
    }

}
